//
//  ShowQRCodeController.m
//  QRMaster
//
//  Created by myApple on 15/7/1.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "ShowQRCodeController.h"
#import "PromptInfoView.h"
#import "BackBtn.h"

@interface ShowQRCodeController ()<UIWebViewDelegate>
{
    UIButton * copyBtn;
}

@end

@implementation ShowQRCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self prepareView];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = nil;
    UIButton *button = [BackBtn initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f) withImage:@"24x24"];
    [button addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftitem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftitem;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [copyBtn setTitle:NSLocalizedString(@"copyText", nil) forState:0];
    self.title = NSLocalizedString(@"scan", nil);
}

-(void)popBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = 40;

    UILabel * QRCodeString  = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    if ([_readerQRCodeString hasPrefix:@"http"]) {
        //直接加载网页
        [self webView:_readerQRCodeString];
    }else
    {
        QRCodeString.text = _readerQRCodeString;
        QRCodeString.font = [UIFont systemFontOfSize:14.0];
    }
    [self.view addSubview:QRCodeString];
    
    
    x = ScreenWidth / 2.0 - 50;
    width = 100;
    height = 40;
    y = UI_SCREEN_HEIGHT - 150;
    
    copyBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    copyBtn.frame = CGRectMake(x, y, width, height);
    [copyBtn setTitle:NSLocalizedString(@"copyText", nil) forState:0];
    [copyBtn setTitleColor:[UIColor whiteColor] forState:0];
    [copyBtn setTitleColor:RGBA(237, 238, 241, 1) forState:UIControlStateHighlighted];
    [copyBtn setBackgroundColor:[UIColor blackColor]];
    [copyBtn addTarget:self action:@selector(copyText:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:copyBtn];
    
}

-(void)copyText:(UIButton *)btn
{
    UIPasteboard *generalPasteBoard = [UIPasteboard generalPasteboard];
    [generalPasteBoard setString:_readerQRCodeString];
    [PromptInfoView showMessage:NSLocalizedString(@"copySuccess", nil) superView:self.view];
}

-(void)webView:(NSString *)str
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight;
    
    UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    webView.delegate = self;
    [webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:webView];
}

#pragma mark -- webviewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    copyBtn.hidden = YES;
    [self startWaiting:nil color:nil addToView:self.view];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self stopWaiting];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
